import pandas as pd
import numpy as np
from faker import Faker

# 初始化配置
fake = Faker('zh_CN')  # 生成中文假数据
np.random.seed(2025)   # 固定随机种子，确保结果可复现
sample_size = 215      # 生成 215 条数据

# 1. 生成人口统计数据（聚焦中老年人）
data = {
    "性别": np.random.choice(["男", "女"], sample_size, p=[0.48, 0.52]),
    "年龄": np.random.choice(
        ["46-55岁", "56岁及以上"], 
        sample_size, 
        p=[0.6, 0.4]  # 60% 46-55岁，40% 56岁及以上
    ),
    "职业": np.random.choice(
        ["退休人员", "企业员工", "其他"], 
        sample_size, 
        p=[0.5, 0.3, 0.2]
    ),
    "月收入": np.random.choice(
        ["5000元以下", "5000-10000元", "10000-20000元", np.nan], 
        sample_size, 
        p=[0.3, 0.4, 0.2, 0.1]
    ),
    "服务频率": np.random.choice(
        ["每月1次", "每周1次及以上", "每季度1次", "首次体验"], 
        sample_size, 
        p=[0.4, 0.3, 0.2, 0.1]
    )
}

# 2. 服务类型（多选）
service_choices = ["健康咨询", "体检套餐", "慢病管理", "康复护理", "其他"]
data["服务类型"] = [
    ";".join(np.random.choice(service_choices, 
                             np.random.randint(1, 3), 
                             replace=False))
    for _ in range(sample_size)
]

# 3. 量表评分（1-5分）
def generate_scores(mean, std, size):
    return np.clip(np.round(np.random.normal(mean, std, size)), 1, 5).astype(int)

scores = {
    "技术专业性": generate_scores(4.2, 0.7, sample_size),
    "服务效率": generate_scores(4.0, 0.8, sample_size),
    "健康方案": generate_scores(4.1, 0.7, sample_size),
    "专业能力": generate_scores(4.3, 0.6, sample_size),
    "沟通清晰": generate_scores(4.2, 0.7, sample_size),
    "反馈响应": generate_scores(3.8, 0.9, sample_size),
    "环境设备": generate_scores(4.0, 0.8, sample_size),
    "价格合理": generate_scores(3.7, 0.9, sample_size),
    "尊重关怀": generate_scores(4.1, 0.8, sample_size),
    "整体体验": generate_scores(4.0, 0.8, sample_size)
}

for q, score in scores.items():
    data[f"Q{list(scores.keys()).index(q)+1}"] = score

# 4. 品牌认知评分
brand_scores = {
    "品牌认知": generate_scores(3.8, 0.9, sample_size),
    "专业形象": generate_scores(4.0, 0.8, sample_size),
    "信任感": generate_scores(4.1, 0.7, sample_size),
    "价值理念": generate_scores(3.9, 0.8, sample_size),
    "推荐意愿": generate_scores(3.7, 0.9, sample_size),
    "优先选择": generate_scores(3.5, 1.0, sample_size)
}

for q, score in brand_scores.items():
    data[f"B{list(brand_scores.keys()).index(q)+1}"] = score

# 5. 开放题
data["改进建议"] = [
    fake.sentence(nb_words=10) if np.random.rand() > 0.3 else np.nan 
    for _ in range(sample_size)
]
data["品牌优势"] = [
    fake.sentence(nb_words=8) if np.random.rand() > 0.4 else np.nan 
    for _ in range(sample_size)
]
data["改进方面"] = [
    fake.sentence(nb_words=8) if np.random.rand() > 0.4 else np.nan 
    for _ in range(sample_size)
]

# 6. 了解渠道
channels = ["亲友推荐", "社交媒体", "线下广告", "健康讲座", "其他"]
data["了解渠道"] = [
    ";".join(np.random.choice(channels, 
                             np.random.randint(1, 3), 
                             replace=False))
    for _ in range(sample_size)
]

# 创建 DataFrame
df = pd.DataFrame(data)

# 添加 ID 列
df.insert(0, 'ID', [f'USER{1000+i}' for i in range(sample_size)])

# 保存为 Excel（无需 encoding 参数）
df.to_excel('中老年健康服务调查数据.xlsx', index=False)  # ✅ 正确写法

print("Excel 文件已生成：中老年健康服务调查数据.xlsx")